#include <iostream>
#include <boost/filesystem/path.hpp>
#include <string>
#include <fstream>
#include <stdio.h>
#include <math.h>
#include "components/cmdline/cmdargs.h"
#include "components/greta/regexpr2.h"
#include "get_arg.h"
using std::string;
using std::ofstream;
using std::ifstream;
using std::fstream;
using std::cerr;
using std::cout;




//_______________________________________________________________________________________

//		process_command_line_options

//				from command line
//				Created [9/13/2001]

//_______________________________________________________________________________________
bool process_command_line_options(int argc, char* argv[], t_program_args& args)
{
	// optional help directive
	CmdArgUsage opt_help('h', "help", 	"display this message", &cerr);

	// required options
	const unsigned REQUIRED = CmdArg::isREQ | CmdArg::isVALREQ;
	CmdArgStr opt_dbname('d', "dbname", "<string>",
								"PostgreSQL database name. ", REQUIRED);
	CmdArgStr opt_host	('h', "host", "<string>",
								"PostgreSQL database host computer address. ");
	CmdArgStr opt_user	('u', "user", "<string>",
								"PostgreSQL database user name. Defaults to 'postgres'.");
	CmdArgStr opt_password('p', "password", "<string>",
								"PostgreSQL database password.");
	CmdArgStr opt_login('l', "login", "<string>",
								"Other PostgreSQL login parameters in the form of "
								"keyword = value. E.g \"port=1234\"");
	CmdArgStr opt_taxon1	('A', "A_species", "<string>",
								"species A in a set of ortholog pairs. "
								"E.g. human",
								REQUIRED);
	CmdArgStr opt_taxon2	('B', "B_species", "<string>",
								"species B in a set of ortholog pairs. "
								"E.g. mouse",
								REQUIRED);
	CmdArgStr opt_errors	('e', "errors_file", "<string>",
								"file listing errors. "
								"E.g. orthologs.mouse_rat.errors",
								REQUIRED);
	CmdArgStr opt_paml		('p', "paml_path", "<string>",
								"path to yn00. Defaults to /share/tools/lg/paml/yn00 ");
	CmdArgStr opt_tempdir	('t', "temp_directory", "<string>",
								"temp directory where yn00 does its work.");
	CmdArgSet opt_inverse	('i', "inverse",
								"Calculate stats for non_masked regions");
	CmdArgSet opt_masked	('m', "masked",
								"Calculate stats for masked regions");
	CmdArgSet opt_whole		('w', "whole",
								"Calculate stats for whole protein");
	
	
	string exe_file_name =  boost::filesystem::path(*argv).leaf();
	
	// construct command line
	CmdLine  cmdline(exe_file_name.c_str(),
							&opt_dbname,
							&opt_host,
							&opt_user,
							&opt_password,
							&opt_login,
							&opt_paml,
							&opt_taxon1,
							&opt_taxon2,
							&opt_inverse,
							&opt_masked,
							&opt_whole,
							&opt_errors,
							&opt_tempdir,
							0);
	// parse command line
	CmdArgvIter  argv_iter(--argc, ++argv);
	cmdline.description(
		"This programme calculates the KaKs, percentage identity and bitscores "
		"for ortholog sequences with specified gene identifier pairs.\n\n"
		
		"A tab delimited file needs to be supplied from STDIN, containing "
			"the gene_ids of each pair of orthologs, "
			"the masked region (e.g. '0:2|5:7' indicates residues 0->2,5->7 inclusive), "
			"and an optional description of the sequence\n\n"
		"The resulting output is sent to STDOUT");
	if (cmdline.parse(argv_iter))
		return false;



	
	args.paml		= opt_paml;
	if (! args.paml.length())
	{
		args.paml= "/share/tools/lg/paml/yn00";
	}
	
	args.region = 0;
    args.region |= (opt_inverse ? eINVERSE	: 0);
    args.region |= (opt_masked	? eMASKED	: 0);
    args.region |= (opt_whole	? eWHOLE	: 0);
	
	if (! args.region)
		args.region = eWHOLE;
	
	//____________________________________________________________________________________
	// 		Connect to PostgreSQL
	//____________________________________________________________________________________
	// initialize postgresql login
	args.db_login = "dbname=" +opt_dbname.str();
	
	string user		= opt_user;
	if (opt_user.str().length())
		args.db_login += " user=" + opt_user.str();
	else
		args.db_login += " user=postgres";
	
	if (opt_host.str().length())
		args.db_login += " host=" + opt_host.str();
	
	if (opt_password.str().length())
		args.db_login += " password=" + opt_password.str();

	if (opt_login.str().length())
		args.db_login += ' ' + opt_login.str();
	
	args.error_file = opt_errors;
	args.tempdir	= opt_tempdir;
	args.taxon1 = opt_taxon1;
	args.taxon2 = opt_taxon2;
	if (args.taxon1 == args.taxon2)
	{
		cerr	<< "\nError!! Species A and B must be two different species!\n\n";
		cmdline.usage(cerr);
		return false;
	}
	if (args.taxon1 > args.taxon2)
		args.taxon1.swap(args.taxon2);
	

	return true;
}






